#include "composite.h"
#include <list>
#include <string>
#include "javabean.h"
#include <iostream>

using namespace std;


class IFile{
public:
    virtual ~IFile()=0;
    virtual void display()=0;
    virtual int add(IFile* file)=0;
    virtual int remove(IFile* file)=0;
    virtual list<IFile*>* getChild()=0;
};

class File:public IFile{
    PointerPropertyBuilderByName(list<IFile*>,list,private)
    PropertyBuilderByName(string,name,private)
public:
    File(string name){
        //文件是不允许有list的
        this->m_list=NULL;
        this->m_name=name;
    }
    ~File(){
        if(this->m_list!=NULL){
            delete m_list;
        }
    }
    virtual void display() {
        cout<<this->m_name<<endl;
    }
    virtual int add(IFile *file){
        return -1;
    }
    int remove(IFile *file){
        return -1;
    }
    list<IFile *> *getChild(){
        return NULL;
    }
};

class Directory:public IFile{
    PointerPropertyBuilderByName(list<IFile*>,list,private)
    PropertyBuilderByName(string,name,private)
public:
    Directory(string name){
        this->m_name=name;
        this->m_list=new list<IFile*>;
    }
    ~Directory(){
        if(m_list!=NULL){
            delete this->m_list;
            this->m_list=NULL;
        }

    }
    void display(){
        cout<<this->m_name;
    }
    int add(IFile *file){
        this->m_list->push_back(file);
        return 0;
    }
    int remove(IFile *file){
        this->m_list->remove(file);
        return 0;
    }
    list<IFile *> *getChild(){
        return this->m_list;
    }
};

void showTree(IFile *file,int level){
    list<IFile*>* l=NULL;
    int i=0;
    for(int i=0;i<level;i++){
        printf("\t");
    }
    file->display();
    l=file->getChild();
    if(l!=NULL){

        for(list<IFile*>::iterator it=l->begin();it!=l->end();it++){
            if((*it)->getChild()==NULL){
                for(i=0;i<=level;i++){
                    printf("\t");
                }
                (*it)->display();
            }else{
                showTree((*it),level+1);
            }
        }
    }

}
